<?php

class DefaultController extends MyAdminController
{
	public $modelName = 'User';
	public $scenario = 'admin';
	
	public function actionIndex()
	{
		$model = new $this->modelName('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET[$this->modelName]))
			$model->attributes=$_GET[$this->modelName];
		$this->render('index', array_merge(array('model'=>$model)));
	}

//	public function loadModel($id)
//	{
//		$model = User::model()->findByPk((int)$id);
//		if($model===null)
//			throw new CHttpException(404,'The requested page does not exist.');
//		return $model;
//	}

	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			Yii::app()->user->setFlash('success', tt('News item successfully deleted'));
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	public function actionCreate()
	{
		$model = new $this->modelName;
		if($this->scenario){
			$model->scenario = $this->scenario;
		}

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST[$this->modelName]))
		{
			$model->attributes=$_POST[$this->modelName];
			if($model->validate()){
				$model->setPassword();
				$model->save(false);
				Yii::app()->user->setFlash('success', tt('User successfully created'));
				$this->redirect(array('index'));
			}
		}

		$this->render('create',array('model'=>$model));
	}
	
	public function actionUpdate($id)
	{
		$model = $this->loadModel($id);
		$model->scenario = 'update';

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST[$this->modelName]))
		{
			$model->attributes = $_POST[$this->modelName];
			if($model->save())
			{
				Yii::app()->user->setFlash('success', tt('User successfully updated'));
				$this->redirect(array('index'));
			}
		}

		$this->render('update',array('model'=>$model));
	}
	
	
	
}